#!/usr/bin/env python

from __future__ import with_statement

import wx
import app
import time
import dialog
import component

import resource as r
import __main as main

class Main(component.Presenter):
    '''Main class
    
    @author Julot <andy.julot@gmail.com>
    @since Saturday, January 14, 2012. 09:07 AM'''
    
    def __init__(self, view, interactor):
        super(Main, self).__init__(view, interactor)
        self.contacts = main.Contacts(view, interactor)
        self.messages = main.Messages(view, interactor)
        self.message = main.Message(view, interactor)
        self.groups = main.Groups(view, interactor)
        self.progress = main.Progress(view, interactor)
        
        self.readLastPage()
        self.readSashPos()

    def close(self):
        self.view.Close()
    
    _about = None
    
    def showAbout(self):
        if not self._about: self._about = dialog.About(self.view)
        self._about.Center()
        self._about.Show()

    def readSashPos(self):
        config = app.Config()
        
        key = '%s/Sash' % self.view.Name
        pos = config.ReadInt(key, 340)
        self.view.splitter.SashPosition = pos
        
        key = '%s/MessagesSashV' % self.view.Name
        pos = config.ReadInt(key, -1)
        if pos != -1: self.view.messages.vSplitter.SashPosition = pos
        
        key = '%s/MessagesSashH' % self.view.Name
        pos = config.ReadInt(key, -1)
        if pos != -1: self.view.messages.hSplitter.SetSashPosition(0, pos)
        
        key = '%s/ContactsSash' % self.view.Name
        pos = config.ReadInt(key, -1)
        if pos != -1: self.view.contacts.splitter.SashPosition = pos
        
        key = '%s/GroupsSash' % self.view.Name
        pos = config.ReadInt(key, -1)
        if pos != -1: self.view.groups.splitter.SashPosition = pos
    
    def readLastPage(self):
        config = app.Config()
        key = '%s/Page' % self.view.Name
        index = config.ReadInt(key, 0)
        self.view.notebook.SetSelection(index)

    def onClose(self):
        self.progress.stopped = True
        self.view.busyInfo('Closing. Please wait...')
        while self.progress.busy:
            time.sleep(1)
        self.view.idle()
        super(Main, self).onClose()
        del self.progress
        self.writeSashPos()
        self.writeLastPage()

    def writeSashPos(self):
        config = app.Config()
        
        key = '%s/Sash' % self.view.Name
        config.WriteInt(key, self.view.splitter.SashPosition)
        
        key = '%s/MessagesSashV' % self.view.Name
        config.WriteInt(key, self.view.messages.vSplitter.SashPosition)
        
        key = '%s/MessagesSashH' % self.view.Name
        config.WriteInt(key, self.view.messages.hSplitter.GetSashPosition(0))
        
        key = '%s/ContactsSash' % self.view.Name
        config.WriteInt(key, self.view.contacts.splitter.SashPosition)
        
        key = '%s/GroupsSash' % self.view.Name
        config.WriteInt(key, self.view.groups.splitter.SashPosition)
    
    def writeLastPage(self):
        config = app.Config()
        key = '%s/Page' % self.view.Name
        config.WriteInt(key, self.view.notebook.Selection)
        
    _virginContacts = True
    
    def changePage(self, old, new):
        menu = self.view.MenuBar
        
        if old == 0:
            id = menu.FindMenu(r.string.menu.main.message.label)
            menu.EnableTop(id, False)
        elif old == 1:
            pass
        elif old == 2:
            pass
        elif old == 3:
            pass
        
        if new == 0:
            id = menu.FindMenu(r.string.menu.main.message.label)
            menu.EnableTop(id, True)
        elif new == 1:
            if self._virginContacts:
                self.contacts.filter()
                self._virginContacts = False
        elif new == 2:
            self.groups.populate()
        elif new == 3:
            pass
    
